<?php
declare(strict_types=1);

namespace SixShop\Lakala\Service;

use SixShop\Lakala\Config;
use SixShop\Lakala\OpenAPISDK\V2\Api\V2LakalaApi;
use SixShop\Lakala\OpenAPISDK\V2\Model\V2ModelRequest;
use SixShop\Payment\Enum\NumberBizEnum;
use function SixShop\Core\throw_logic_exception;

/**
 * LedgerService
 */
class LedgerService
{
    private V2LakalaApi $v2LakalaApi;

    public function __construct(private Config $config)
    {
        $this->v2LakalaApi = new V2LakalaApi($config->getV2Config());
    }

    /**
     * 分账接收方创建申请
     *
     * @link https://o.lakala.com/#/home/document/detail?id=382
     */
    public function applyLedgerReceiver(array $reqData, string $version = '1.0'):object
    {
        $request = new V2ModelRequest();
        $reqData['version'] = $version;
        $request->setReqData($reqData);
        $response = $this->v2LakalaApi->tradeApi('/api/v2/mms/openApi/ledger/applyLedgerReceiver', $request);
        if ($response->getRetCode() == '000000') {
            return $response->getRespData();
        } else {
            throw_logic_exception(
                msg:$response->getRetMsg(),
                code: (int)$response->getRetCode(),
                data: $response->getRespData(),
            );
        }
    }

    /**
     * 分账接收方信息变更
     *
     * @link https://o.lakala.com/#/home/document/detail?id=383
     */
    public function modifyLedgerReceiver(array $reqData, string $version = '1.0')
    {
        $request = new V2ModelRequest();
        $reqData['version'] = $version;
        $request->setReqData($reqData);
        $response = $this->v2LakalaApi->tradeApi('/api/v2/mms/openApi/ledger/modifyLedgerReceiver', $request);
        if ($response->getRetCode() == '000000') {
            return $response->getRespData();
        } else {
            throw_logic_exception(
                msg:$response->getRetMsg(),
                code: (int)$response->getRetCode(),
                data: $response->getRespData(),
            );
        }
    }

    /**
     * 分账接收方详情查询
     *
     * @param string $orderNo 订单号
     * @param string $receiverNo 接收方编号
     * @param string $orgCode 机构编号
     * @param string $version 接口版本号 默认1.0
     *
     * @link https://o.lakala.com/#/home/document/detail?id=385
     */
    public function queryReceiverDetail(string $orderNo, string  $receiverNo, string $orgCode = '1', string $version = '1.0')
    {
        $request = new V2ModelRequest();
        $request->setReqData([
            'version' => $version,
            'orderNo' => $orderNo,
            'orgCode' => $orgCode,
            'receiverNo' => $receiverNo,
        ]);
        $response = $this->v2LakalaApi->tradeApi('/api/v2/mms/openApi/ledger/queryReceiverDetail', $request);
        if ($response->getRetCode() == '000000') {
            return $response->getRespData();
        } else {
            throw_logic_exception(
                msg:$response->getRetMsg(),
                code: (int)$response->getRetCode(),
                data: $response->getRespData(),
            );
        }
    }

    /**
     * 分账关系绑定申请
     *
     * @param array $reqData
     * @param string $version
     * @link https://o.lakala.com/#/home/document/detail?id=386
     */
    public  function applyBind(array $reqData, string $version = '1.0'): object
    {
        $request = new V2ModelRequest();
        $reqData['version'] = $version;
        $reqData['merCupNo'] = $this->config->merchant_no;
        $reqData['retUrl'] = $this->config->notify_url;
        $request->setReqData($reqData);
        $response = $this->v2LakalaApi->tradeApi('/api/v2/mms/openApi/ledger/applyBind', $request);
        if ($response->getRetCode() == '000000') {
            return $response->getRespData();
        } else {
            throw_logic_exception(
                msg:$response->getRetMsg(),
                code: (int)$response->getRetCode(),
                data: $response->getRespData(),
            );
        }
    }

    /**
     * 分账关系申请列表查询
     *
     * @link https://o.lakala.com/#/home/document/detail?id=939
     */
    public function queryBindApplyList(array $reqData, int $pageNum = 1, int $pageSize = 10, string $version = '1.0'):object
    {
        $request = new V2ModelRequest();
        $reqData = array_merge($reqData, [
            'version' => $version,
            'pageNum' => $pageNum,
            'pageSize' => $pageSize,
            'merCupNo' => $this->config->merchant_no,
        ]);
        $request->setReqData($reqData);
        $response = $this->v2LakalaApi->tradeApi('/api/v2/mms/openApi/ledger/openWeb/listRelationApply', $request);
        if ($response->getRetCode() == '000000') {
            return $response->getRespData();
        } else {
            throw_logic_exception(
                msg:$response->getRetMsg(),
                code: (int)$response->getRetCode(),
                data: $response->getRespData(),
            );
        }
    }

    /**
     * 商户分账信息查询
     *
     * @link https://o.lakala.com/#/home/document/detail?id=381
     */
    public function queryLedgerMer(string $merchantNo, string $orgCode = '1', string $version = '1.0'):object
    {
        $request = new V2ModelRequest();
        $request->setReqData([
            'version' => $version,
            'orderNo' => generate_number(NumberBizEnum::ORDER_NO, 5),
            'orgCode' => $orgCode,
            'merCupNo' => $merchantNo,
        ]);
        $response = $this->v2LakalaApi->tradeApi('/api/v2/mms/openApi/ledger/queryLedgerMer', $request);
        if ($response->getRetCode() == '000000') {
            return $response->getRespData();
        } else {
            throw_logic_exception(
                msg:$response->getRetMsg(),
                code: (int)$response->getRetCode(),
                data: $response->getRespData(),
            );
        }
    }
}